package cn.itsource.pet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.GetUserInfoUtil;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.RedisService;
import cn.itsource.pet.domain.SearchMasterMessage;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.SearchMasterMessageMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.query.SearchMasterMessageQuery;
import cn.itsource.pet.service.ISearchMasterMessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
//Propagation.SUPPORTS:支持别人事务
@Transactional
public class SearchMasterMessageServiceImpl extends BaseServiceImpl<SearchMasterMessage, SearchMasterMessageQuery> implements ISearchMasterMessageService {

    @Autowired
    private RedisService redisService;

    @Autowired
    private SearchMasterMessageMapper searchMasterMessageMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Override
    public AjaxResult publish(SearchMasterMessage message) {

        //参数验证自己去验

        //1.userId要关联上去  redis拿到token : userInfo
        Long userId = GetUserInfoUtil.getUserId();
        message.setUserId(userId);
        //2.入库
        searchMasterMessageMapper.insertSelective(message);
        return AjaxResult.createSuccess();
    }

    @Override
    public AjaxResult shenghe(SearchMasterMessage message) {

        //自己去验参

        //1.查询一把之前的数据
        SearchMasterMessage searchMasterMessage = searchMasterMessageMapper.selectByPrimaryKey(message);
        Optional.ofNullable(searchMasterMessage).orElseThrow(() -> new BusinessException("数据跑了,异常"));

        //两种情况
        //2.1直接改searchMasterMessage的状态
        searchMasterMessageMapper.updateByPrimaryKeySelective(message);

        //2.2通过 ,数据要如Msg业务表的库
        if (message.getState() == 1){ //审核通过

            SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.selectByPrimaryKey(message);
            if (searchMasterMsg != null){
                throw new BusinessException("你的技术还可以,但是还不够");
            }

            //Bean copy
            SearchMasterMsg masterMsg = BeanUtil.copyProperties(searchMasterMessage, SearchMasterMsg.class);
            masterMsg.setNote(message.getNote());//摘要
            masterMsg.setState(0);//未接单

            //入库
            searchMasterMsgMapper.insertSelective(masterMsg);

        }

        return AjaxResult.createSuccess();
    }

    @Override
    public PageList<SearchMasterMessage> findPage(SearchMasterMessageQuery messageQuery) {

        //分页插件
        PageHelper.startPage(messageQuery.getCurrentPage(),messageQuery.getPageSize());
        //查询数据
        List<SearchMasterMessage> masterMessages = searchMasterMessageMapper.findPage(messageQuery);

        //封装pageInfo对象
        PageInfo<SearchMasterMessage> pageInfo = new PageInfo<>(masterMessages);
        //返回结果
        return  new PageList<>(pageInfo.getTotal(),pageInfo.getList());


    }
}
